//
// Created by caedmon on 2017/12/15 0015.
//

#include <stdio.h>

#define ROWS 3
#define COLS 5

void write(double (*src)[COLS],int n);
double rowAverage(double (*src)[COLS], int n);
double average(double (*src)[COLS], int n);
double max(double (*src)[COLS],int n);
void display(double (*src)[COLS], double *rowAve, double ave, double maxValue, int n);

void practise_10_13(){
    double arr[ROWS][COLS];
    double Rave[ROWS];
    double rowAve,Ave,Max;
    int i, j;
    write(arr,ROWS);
    for(i = 0; i < ROWS; i++){
        rowAve = rowAverage(arr, i);
        Rave[i] = rowAve;
    }
    Ave = average(arr,ROWS);
    Max = max(arr, ROWS);
    display(arr, Rave, Ave, Max, ROWS);
    printf("\n");
    getchar();
    getchar();
}

void write(double (*src)[COLS], int n){
    int i , j;
    for(i = 0; i < ROWS; i++){
        printf("Please input %d rows number:\n",i);
        for(j = 0; j < COLS; j++){
            scanf("%lf",&src[i][j]);
        }
    }
}

double rowAverage(double (*src)[COLS], int n){
    int j;
    double Raverage, sum;
    sum = 0;
    for(j = 0; j < COLS; j++){
        sum += src[n][j];
    }
    Raverage = sum / COLS;
    return Raverage;
}

double average(double (*src)[COLS], int n){
    int i, j;
    double sum, average;
    sum = 0;
    for(i = 0; i < n; i++){
        for(j = 0; j < COLS; j++){
            sum += src[i][j];
        }
    }
    average = sum/(n*COLS);
    return average;
}
double max(double (*src)[COLS], int n){
    double max = 0;
    int i , j;
    for(i = 0; i < n; i++){
        for(j = 0; j < COLS; j++){
            if(src[i][j] > max){
                max = src[i][j];
            }
        }
    }
    return max;
}

void display(double (*src)[COLS], double *rowAve, double Ave, double maxValue, int n){
    int i, j;
    printf("The array you have entered: \n");
    for(i = 0; i < n; i++){
        for(j = 0; j < COLS; j++){
            printf("%4.1lf", src[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    printf("The every row average of the array you have entered:\n");
    for(i = 0; i < n; i++){
        printf("%4.1f\n", rowAve[i]);
    }
    printf("The average of the array you have entered:\n");
    printf("%4.1f\n", Ave);
    printf("The max value of the array you have entered:\n");
    printf("%4.1f\n", maxValue);
}